package com.ykqx.weather.app.controller;

import com.ykqx.weather.app.util.dateutil.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/ykqx")
public class YKPreServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /*
     * private String filePath=""; private String fileName =
     * "http://122.224.174.179:9990/pic/rain1";
     */
    private String parseDate;// 解析后的时间
    private int offSet;// 偏移量 问杨科长图片命名规则再改
    private String suffix = ".png";// 图片后缀
    private String searchName;// 搜索的图片名字

    public YKPreServlet() {
        super();
    }

    @RequestMapping(value = "/ykPre")
    public void getYKPre(String index, HttpServletResponse response)
            throws ServletException, IOException {
        String dirPath = "D:\\dengzhixian\\tupian\\Pic";// 图片目录 放入方法内防止多次调用叠加路径
        boolean flag = false;// 标记 放入方法内 径行初始化 防止前人挖坑后人跳
        switch (Integer.parseInt(index)) {
            case 0:
                dirPath = dirPath + "\\rain1";
                offSet = 24;
                break;
            case 1:
                dirPath = dirPath + "\\rain2";
                offSet = 24;
                break;
            case 2:
                dirPath = dirPath + "\\rain3";
                offSet = 24;
                break;
            case 3:
                dirPath = dirPath + "\\rain4";
                offSet = 24;
                break;
            case 4:
                dirPath = dirPath + "\\rain5";
                offSet = 24;
                break;
            case 5:
                dirPath = dirPath + "\\rain6";
                offSet = 24;
                break;
            case 6:
                dirPath = dirPath + "\\rain7";
                offSet = 24;
                break;
            case 7:
                dirPath = dirPath + "\\rain8";
                offSet = 24;
                break;
            case 8:
                dirPath = dirPath + "\\rain9";
                offSet = 24;
                break;
            case 9:
                dirPath = dirPath + "\\rain10";
                offSet = 24;
                break;
            case 10:
                dirPath = dirPath + "\\rain11";
                break;
            default:
                break;
        }
        System.out.println("文件夹：" + dirPath);
        File file = null;
        Date date = new Date();
        File dir = new File(dirPath);
        if (!dir.exists()) {
            throw new IllegalArgumentException(dir + "不存在");
        }
        if (dir.isFile()) {
            throw new IllegalArgumentException(dir + "不是目录");
        }
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < offSet; i++) {// 这层在外面才能找到最新的

                parseDate = DateUtil.subHour(date, i);
                for (int j = 0; j < files.length; j++) {
                    searchName = parseDate + suffix;
                    System.out.println("搜索名: " + searchName);
                    File mFile = files[j];
                    System.out.println("文件名: " + mFile.getName());
                    if (mFile.getName().equals(searchName)) {
                        file = mFile;
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    break;
                }
                //break; 循环还有问题  找到了 还会再走外层
            }
        }
        /*
		 * URL url = new
		 * URL("http://122.224.174.179:9990/pic/rain1/20170515170000.png");
		 * InputStream is = url.openStream();
		 */
        if (file != null) {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buf = new byte[100 * 1024];
            int len;
            while ((len = bis.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
                outputStream.flush();
            }
            fis.close();
            bis.close();
            outputStream.close();
        }
    }


}
